-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to forbid empty test suites #4123
Conversation
@eemeli I apalogize for not responding to your PR. Are you still interested? |
Yes, I'm still interested in this. Happy to have this reviewed and hopefully merged! |
What about
We could end up with zero tests run, but the script would still pass. Some users also create tests dynamically at runtime, when the parsing/creating of the test runner has already been completed. |
I would argue that a suite with a skipped test is not "empty", and that the right thing in that case is not to complain. Sure, skipping all tests would therefore result in a suite passing even if none of the tests run, but that'd be intentional in this case. This logic also allows for using |
Regarding dynamically generated tests, that's an entirely valid point. I don't think the proposed behaviour would be very surprising in that case, but that should probably still be accounted for. Are there other places than |
I don't understand this one:
With dynamically generated tests I was refering to scenarios like #4126 etc. You can do this in
At suite-end event, I guess. In your issue you wrote:
In this PR you switched to checking each and every suite, instead of a final test at I'm not sure yet, wether we should go your way, or check once at |
Perhaps I should rephrase the option to consider it an error if a suite "defines" no tests, rather than "containing" no tests? That might make it altogether clearer. Regarding
Expect for the
Three reasons:
I'll amend and rebase this PR on top of the latest master, to account for dynamically added tests. |
On an entirely different note, I rather appreciate the timing of pointing at an issue filed on the last Friday the 13th, on a Friday the 13th. |
Yes, and now the corona virus. Will there be any survivors left for using your new feature?
No, a |
Well, haven't we all heard recently that testing in particular is rather important just now?
You're right, that was a misunderstanding on my part. I added a test for a dynamically-defined test in an otherwise empty suite, and it turns out that the current approach already handles that case just like it should. And then I realised that I can use the already-calculated And therefore I didn't move the test to the suite-end, because the existing behaviour after those lines prevents the suite-begin and suite-end events from being emitted if the suite is empty, and there's no reason to change the existing behaviour. I also clarified the docs a little bit, and rebased the branch on the current |
@eemeli I will review this weekend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eemeli I don't feel good with this implementation:
- I still haven't understood why you switched
- from focus test run:
"0 passing" tests to be considered an error
- to focus suite:
an empty suite with no tests fails
even with a next suite containing tests.
- from focus test run:
Prefer to fail earlier rather than later
: do you want to bail/abort after first empty suite or continue?
The first suite fails (in an incorrect manner), but the test run keeps going. Adding more empty suites, will throw more exceptions. But at the end we have passing tests and a failing test run.
describe("suite", function(){
describe('inner empty suite', function() { });
describe('inner full suite', function() {
it("test 1", function(){ });
it("test 2", function(){ });
});
});
- the
total
way is not consistent:- suites with skipped tests only => pass
- suites with tests but not run (
grep
not matched) => fail
@@ -732,6 +732,9 @@ Runner.prototype.runSuite = function(suite, fn) { | |||
|
|||
debug('run suite %s', suite.fullTitle()); | |||
|
|||
if (!total && this.forbidEmptySuite) { | |||
this.fail(suite, new Error('Empty suite forbidden')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suite
does not extend Runnable
(as does Test
and Hook
), so you can't fail
a Suite
.
The reporter output is weird, if the test file contains only an empty suite.
Maybe a dummy test?
@eemeli Are you still interested in working on this? |
FWIW I think this is a reasonable addition. It's not something that can be discovered via static analysis, and people do want to be able to run Mocha w/o this sort of restriction. At this point though I wonder if we should add something like a "CI mode" which enables various "strict" behaviors... |
Sorry, this fell off my radar. Yes, I'm still interested in working on this. I'll need to review @juergba's comments and get back to you on this. |
Closing, as I clearly haven't had the time to work on this. |
Description of the Change
Adds an option
forbidEmptySuite
, available in the CLI as--forbid-empty-suite
, that will fail a suite with an errorEmpty suite forbidden
if it contains no tests.Alternate Designs
A similar result could be achieved by an individual customised reporter, but that seems like the wrong layer at which to address this.
Why should this be in core?
Because the other options are also defined in the core.
Benefits
At the moment, if the test definitions fail in a context that does not fail a subsequent
mocha.run()
(e.g. a browser processing multiple<script>
tags), this failure may not be caught by Mocha, and instead it will erroneously display a positive result with "0 passing" tests. This is problematic in particular in CI tests, where the results are often left unchecked if they "pass".Possible Drawbacks
This adds yet another option.
Applicable issues
Fixes #4062